from django.shortcuts import render
from django.shortcuts import redirect
from django.views import generic

from cart.services import cart
from . import forms


class ShowingCart(generic.View):

    """ Показываем корзину пользователя """

    def get(self, request):

        user_cart = cart.Cart(request)
        user_cart.add_quantity_update()
        context = {'user_cart': user_cart}

        return render(request, 'cart/show.html', context)


class AddingProductToCart(generic.View):

    """ Добавляем продукт в корзину """

    def post(self, request, slug):

        bound_form = forms.AddingProductToCartForm(request.POST)

        if bound_form.is_valid():
            cd = bound_form.cleaned_data
            print(f'cd - {cd}')
            quantity, update_quantity = cd['quantity'], cd['update_quantity']
            user_cart = cart.Cart(request)
            user_cart.add_product(slug, quantity, update_quantity)

            return redirect('cart:show_cart')


class RemovingProduct(generic.View):

    """ Удаляем продукт из корзины """

    def get(self, request, slug):

        user_cart = cart.Cart(request)
        user_cart.remove_product(slug)
        return redirect('cart:show_cart')
